﻿package com.projects.xenia.journals_activities;
import java.io.File;
import java.io.FileFilter;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.widget.Toast;
import android.widget.SimpleCursorAdapter.CursorToStringConverter;

import com.projects.xenia.R;
import com.projects.xenia.documents_activities.Activity_PhotoReport;
import com.projects.xenia.documents_classes.Documents_PhotoReport;
import com.projects.xenia.documents_classes.Documents_Visit;
import com.projects.xenia.global_modules.MainGlobalModule;

public class Activity_Journal_PhotoReports extends JournalActivity {
	
	@Override
	public void SetIconAndDescr() {
		
		head_ico.setImageResource(R.drawable.st_photos);
		head_text.setText("Фотоотчеты");
		
	}
	
	@Override
	public void GetJournalCursor () {		
	
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");		
		String JournalStartDateValueString = sdf.format(JournalStartDateValue);
		String JournalStopDateValueString = sdf.format(JournalStopDateValue);
		JournalCursor = MainGlobalModule.DBE.rawQuery("select a.id,a.docnum,date(a.date_create) as date_create ,a.state,a.customers_id,0,b.descr,b.outletdescr,b.outletadress from xdoc_PhotoReports_h a left outer join xref_customers b on a.customers_id = b.id where date(a.date_create) between date(?) and date(?) order by date(a.date_create),a.docnum", new String []{JournalStartDateValueString,JournalStopDateValueString});
		
	}
	
    @Override
	public void StartDocumentsDetailsJournalActivity() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void CreateOpenDocument() {
		
		if (Journ_Customer.getTag()!=null) {
			Documents_Visit DocVisit = new Documents_Visit ();
			if (DocVisit.CheckNewVisitsExistsByCustomerId(String.valueOf(Journ_Customer.getTag()))) {
				Toast.makeText(this, "Нелья создать документ вне визита !!!", Toast.LENGTH_SHORT).show();
				return;
			}		
		}
		
		Intent DocumentActivity = new Intent ();    	
    	DocumentActivity.setClass(Ctx, Activity_PhotoReport.class);
    	DocumentActivity.putExtra("CustomerId", Journ_Customer.getTag()==null?null:String.valueOf(Journ_Customer.getTag()));
    	((Activity)Ctx).startActivityForResult(DocumentActivity, 0);
    	
	}

	@Override
	public void OpenDocument() {

		Intent DocumentActivity = new Intent ();    	
    	DocumentActivity.setClass(Ctx, Activity_PhotoReport.class);
    	DocumentActivity.putExtra("DocId", id);    	
    	((Activity)Ctx).startActivityForResult(DocumentActivity, 0);
		
	}

	@Override
	public void DeleteDocument() {
		
		Documents_PhotoReport Doc = new Documents_PhotoReport ();
		Doc.OpenDoc(id);
		Doc.DeleteDoc();
		RefreshList();
		
	}

	@Override
	public void JournIcoEvents() {
		
		
		
		AlertDialog.Builder adb = new AlertDialog.Builder (Ctx);
		adb.setItems(new String [] {"Отправить изображения повторно","Удалить неиспользуемые изображения"}, new OnClickListener() {			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				int count = 0;
				
				switch (which) {
				
				case 0:
					
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");		
					String JournalStartDateValueString = sdf.format(JournalStartDateValue);
					String JournalStopDateValueString = sdf.format(JournalStopDateValue);
					
					ContentValues UpdateValues = new ContentValues();
					UpdateValues.put("sended", 0);
										
					count = MainGlobalModule.DBE.update("xdoc_photoreports_t", UpdateValues, "hdoc_id in (select id from xdoc_photoreports_h where date (date_create) between date (?) and date (?))", new String []{JournalStartDateValueString, JournalStopDateValueString});
					if (count>0) {
						Toast.makeText(Ctx, String.format("%d файлов изображений в очереди к отправке", count), Toast.LENGTH_SHORT).show();
					}
					
					break;

				case 1:
					
					Cursor PhotosC = MainGlobalModule.DBE.rawQuery("select id from xdoc_photoreports_t", null);
					HashMap<String, String> PhotosL = new HashMap<String, String> ();
					if (PhotosC.getCount()>0) {
						PhotosC.moveToFirst();
						while (!PhotosC.isAfterLast()) {
							String photo_id = PhotosC.getString(0);
							String photo_file = String.format("%s.jpg", photo_id);
							PhotosL.put(photo_file, photo_id);
							PhotosC.moveToNext();
						}
					};
					PhotosC.close();				
					
					File ff = new File(MainGlobalModule.PhotoReportsImagesPath);
					File [] photof = ff.listFiles();
					count = 0;									
					for (File photoit:photof) {						
						if (!PhotosL.containsKey(photoit.getName())) {
							photoit.delete();
							count ++;
						}
					}
					
					if (count>0) {
						Toast.makeText(Ctx, String.format("Удалено %d файлов изображений.", count), Toast.LENGTH_SHORT).show();
					}
					
				default:
					break;
				}
				
				dialog.dismiss();
			}
		});
		
		adb.create().show();

		
	}
	
}
